package com.atguigu.webadmin.controller.system;

import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.SystemPost;
import com.atguigu.model.enums.BaseStatus;
import com.atguigu.webadmin.service.SystemPostService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Tag(name = "后台用户岗位管理")
@RequestMapping("/admin/system/post")
@CrossOrigin
public class SystemPostController {

    @Autowired
    private SystemPostService systemPostService;

    @Operation(summary = "分页获取岗位信息")
    @Parameters({
            @Parameter(name = "current",description = "当前页码",in = ParameterIn.QUERY),
            @Parameter(name = "size",description = "查询数据",in = ParameterIn.QUERY)
    })
    @GetMapping("page")
    private Result<IPage<SystemPost>> page(@RequestParam long current, @RequestParam long size) {
        //创建分页查询类
        IPage<SystemPost> iPage = new Page<>(current,size);
        //执行查询
        IPage<SystemPost> page = systemPostService.page(iPage);
        //返回查询结果
        return Result.ok(page);
    }

    @Operation(summary = "保存或更新岗位信息")
    @Parameters({
            @Parameter(name = "systemPost",description = "岗位信息实体类",in = ParameterIn.DEFAULT)
    })
    @PostMapping("saveOrUpdate")
    public Result<Boolean> saveOrUpdate(@RequestBody SystemPost systemPost) {
        boolean b = systemPostService.saveOrUpdate(systemPost);
        return Result.ok(b);
    }

    @DeleteMapping("deleteById")
    @Parameters({
            @Parameter(name = "id",description = "岗位id",in = ParameterIn.QUERY)
    })
    @Operation(summary = "根据id删除岗位")
    public Result<Boolean> removeById(@RequestParam Long id) {
        boolean b = systemPostService.removeById(id);
        return Result.ok(b);
    }

    @GetMapping("getById")
    @Parameters({
            @Parameter(name = "id",description = "岗位id",in = ParameterIn.QUERY)
    })
    @Operation(summary = "根据id获取岗位信息")
    public Result<SystemPost> getById(@RequestParam Long id) {
        SystemPost byId = systemPostService.getById(id);
        return Result.ok(byId);
    }

    @Operation(summary = "获取全部岗位列表")
    @GetMapping("list")
    public Result<List<SystemPost>> list() {
        List<SystemPost> systemPostList = systemPostService.list();
        return Result.ok(systemPostList);
    }

    @Operation(summary = "根据岗位id修改状态")
    @Parameters({
            @Parameter(name = "id",description = "岗位id",in = ParameterIn.QUERY),
            @Parameter(name = "status",description = "岗位状态",in = ParameterIn.DEFAULT)
    })
    @PostMapping("updateStatusByPostId")
    public Result<Boolean> updateStatusByPostId(@RequestParam Long id,
                                       @RequestParam BaseStatus status) {
        //创建lambda更新包装类
        LambdaUpdateWrapper<SystemPost> updateWrapper = new LambdaUpdateWrapper<>();
        //指定查询条件
        updateWrapper.eq(id != null,SystemPost::getId,id);
        updateWrapper.set(SystemPost::getStatus,status);
        boolean update = systemPostService.update(updateWrapper);
        return Result.ok(update);
    }
}
